#include "ioUtil.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
nInt saveAllBytes(const char* fileName, byte* buffer, nInt size) {
	FILE* f;
	fopen_s(&f, fileName, "w+");
	if (f == NULL) {
		return 1;
	}
	fwrite(buffer, 1, size, f);
	fclose(f);
	return 0;
}
nInt readAllBytes(const char* fileName, byte** buffer, nInt* size) {
	*size = 0;

	FILE* fd;
	fopen_s(&fd, fileName, "rb");
	if (fd == NULL) {
		return 1;
	}
	fseek(fd, 0, SEEK_END);
	nInt len = ftell(fd);
	fseek(fd, 0, SEEK_SET);
	byte* buf = (byte*)malloc(len);
	if (buf == NULL) {
		fclose(fd);
		return 2;
	}
	nInt rlen = (int)fread_s((void*)buf, len, 1, len, fd);
	fclose(fd);
	*size = rlen;
	*buffer = buf;
	return 0;
}